import 'dart:io';

serverTest() async {
  final requests = await HttpServer.bind('localhost', 8888);
  await for (var request in requests) {
    processRequest(request);
  }
}

String getPath(String p) {
  return Directory.current.path +
      Platform.pathSeparator +
      'std' +
      Platform.pathSeparator +
      'io' +
      Platform.pathSeparator +
      p.substring(p.lastIndexOf('/') + 1);
}

void processRequest(HttpRequest request) {
  print('Got request for ${request.uri.path}');
  final response = request.response;
  if (request.uri.path == '/dart') {
    response
      ..headers.contentType = ContentType(
        'text',
        'plain',
      )
      ..write('Hello from the server');
  } else if (request.uri.path.startsWith('/assert')) {
    print(getPath(request.uri.path));
    File file = File(getPath(request.uri.path));
    if (file.existsSync()) {
      response
        ..statusCode = HttpStatus.ok
        ..write(file.readAsStringSync());
    } else {
      response
        ..statusCode = HttpStatus.notFound
        ..write('${request.uri.path} not found');
    }
  } else if (request.uri.path == '/') {
    response
      ..statusCode = HttpStatus.ok
      ..write('hello dart');
  } else {
    response
      ..statusCode = HttpStatus.internalServerError
      ..write(' ');
  }
  response.close();
}

void getIp() async {
  // 内网ip
  for (var interface in await NetworkInterface.list()) {
    for (var addr in interface.addresses) {
      print('${addr.address}');
      print('${addr.address.split(".")}');
    }
  }
}

main() {
  //serverTest();
  getIp();
}
